/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.algorithms.mcorrea;

/**
 *
 * @author mcorrea
 */
public class TwoComplementCodeSprint2011 {

	//runs in O(n2), not optimal
	public static void main(String args[]) {
		int a = -1;
		int b = 4;
		System.out.println(countones(a, b));
	

	}

	
	public static int count(int x) {

		int count = 0;
		if (x == 0) {
			return 0;
		}
	
		String binary = Integer.toBinaryString(x);
		for (char c : binary.toCharArray()) {
			if (c == '1') {
				count++;
			}
		}
		return count;

	}

	public static int countones(int a, int b) {
		
		int total = 0;
		for(int k=a; k<=b; k++){
			total+=count(k);
		}

		return total;
	}
}
